<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="content-type" content="no-cache, must-revalidate"/>
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT"/>
    <title>菜谱信息详情</title>

    <link href="css/bootstrap.min.css" rel="stylesheet"/>
    <link href="css/my.css" rel="stylesheet"/>
    <link href="css/nav.css" rel="stylesheet">
    <link href="/end/css/quill.snow.css" rel="stylesheet">
    <link href="css/element/index.css" rel="stylesheet"/>

    <style>
        [v-cloak] {
            display: none;
        }
        .ql-editor img {
            width: 600px;
        }
    </style>

</head>
<body>
<div id="wrapper" v-cloak>
    <div class="container">
        <!-- 头部开始 -->
        <div class="nx-header">
            <div style="display: flex;width: 100%; height: 30px; line-height: 30px; background-color: #eee;">
                <div style="flex: 5; padding: 0 10px; color: orangered">
                    欢迎来到舒乐美食！
                </div>
                <div style="flex: 5; text-align: right">
                    <span v-if="user.name">
                    欢迎您，{{user.name}}
                    <a style="margin-right: 50px; color: blue" href="javascript:void(0)" @click="logout">退出</a>
                    </span>
                    <a v-if="!(user.name)" href="/end/page/login.html" target="_blank" style="margin-right: 10px; color: #666666">登录</a>
                    <a href="/end/page/register.html" target="_blank" style="margin-right: 30px; color: #666666">注册</a>
                </div>
            </div>

            <div style="height: 100px; padding-left: 10px; display: flex; justify-content: left;align-items: center;
                    color: orangered; font-size: 30px; font-family: 微软雅黑">
                舒乐美食
            </div>

            <div>
                <ul style="display: flex; background-color: orange; height: 30px">
                    <li class="nav-item"><a href="index.html">首页</a></li>
                    <li class="nav-item"><a href="advertiserInfo.html">系统公告</a></li>
					<li class="nav-item"><a class="active" href="foodsMenuInfo.html">菜谱信息</a></li>
<!--					<li class="nav-item"><a href="foodsMaterialInfo.html">食材信息</a></li>-->
					<li class="nav-item"><a href="collectInfo.html">收藏信息</a></li>
					<li class="nav-item"><a href="notesInfo.html">笔记信息</a></li>
					<li class="nav-item"><a href="newsInfo.html">饮食资讯信息</a></li>
					<li class="nav-item"><a href="messageInfo.html">趣味答题信息</a></li>

                    <li v-if="isShow" class="nav-item"><a href="/end/page/index.html" target="_blank">进入后台系统</a></li>
                </ul>
            </div>

        </div>
        <!-- 头部结束 -->
    </div>
    <div class="container" style="margin-top: 10px">
        <div class="info">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="panel-body bg-gray">
							<div class="row p-10">
								<div class="col-md-2">名称：</div>
								<div class="col-md-10">{{ obj.name }}</div>
							</div>
							<div class="row p-10">
								<div class="col-md-2">描述：</div>
								<div class="col-md-10">{{ obj.description }}</div>
							</div>
							<div class="row p-10">
								<div class="col-md-2">菜谱标签：</div>
								<div class="col-md-10">{{ obj.tips }}</div>
							</div>
							<div class="row p-10">
								<div class="col-md-2">上传人：</div>
								<div class="col-md-10">{{ obj.userName }}</div>
							</div>
							<div class="row p-10">
								<div class="col-md-2">菜谱图片：</div>
								<div class="col-md-10">
									<img v-if="obj.fileType === 'image'" :style="'width:' + imgWidth" :src="fileSrc" alt="">
									<div v-if="obj.fileType === 'video'"><video controls="controls" :src="fileSrc" style="width: 100%"></video></div>
									<div v-if="obj.fileType === 'other'">该文件不支持预览，只有视频和图片支持预览</div>
								</div>
							</div>

                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div style="text-align: center">
            <button class="btn btn-sm" style="background-color: red; color: white" @click="praise()">
                点赞({{obj.praiseCount}})
            </button>
            <button class="btn btn-sm" style="background-color: darkorange; color: white" @click="collect()">
                收藏({{obj.collectCount}})
            </button>
        </div>
        <!-- /. 表单数据 -->

        <div class="comment" v-if="isComment">
            <div class="row">
                <div class="col-md-8 col-md-offset-2">
                    <h2>评论</h2>
                </div>
            </div>


            <div class="comment-item">
                <!--                <div class="row mt-10">-->
                <!--                    <div class="col-md-8 col-md-offset-2" style="font-size: 20px; font-weight: bold">admin</div>-->
                <!--                </div>-->
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: justify">
                        <textarea style="resize:none; width: 100%" rows="3" v-model="comment.content"></textarea>
                    </div>
                </div>
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: right;">
                        <button class="btn btn-primary f-r" @click="add()">发送</button>
                        <hr>
                    </div>
                </div>
            </div>


            <div class="comment-item" v-for="item in comments">
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="font-size: 20px; font-weight: bold">{{ item.name }}
                    </div>
                </div>
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: justify">
                        {{ item.content }}
                    </div>
                </div>
                <div class="row mt-10">
                    <div class="col-md-8 col-md-offset-2" style="text-align: right;">
                        <span style="color: #888888">{{ item.time }}</span>
                        <hr>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/vue2.6.11/vue.min.js"></script>
<script src="js/vue2.6.11/axios.js"></script>
<script src="/end/js/quill.js"></script>
<script src="js/element/index.js"></script>

<script>
    function msg(type, msg) {
        Vue.prototype.$message({
            type: type, // success（成功）、warning（警告）, error(错误)
            message: msg,
            duration: 2000,
            offset: 100,
            center: true,
        })
    }
    new Vue({
        el: '#wrapper',
        data: {
            imgWidth: '0px',
            comments: [],
            content: '',
            obj: {},
            isComment: false,
            comment: {
                content: '',
                foreignId: 0
            },
            fileSrc: '',
            user: {},
            isShow: false
        },
        created() {
            axios.get('/auth').then(res => {
                if (res.data.code === '0') {
                    this.user = res.data.data;
                    if (this.user.level !== 3) {
                        this.isShow = true;
                    }
                }
            });
            this.load();
        },
        methods: {
            load() {
                let id = this.getQueryVariable('id');
                this.comment.foreignId = id;
                if (id !== 0) {
                    axios.get('/foodsMenuInfo/' + id).then(res => {
                        if (res.data.code === '0') {
                            let obj = res.data.data;
                            this.obj = obj;
                            if (obj.fileName) {
                                if (/\.(png|jpg|gif|jpeg|webp)$/.test(obj.fileName)) {
                                    this.obj.fileType = 'image';
                                } else if (/\.mp4$/.test(obj.fileName)) {
                                    this.obj.fileType = 'video';
                                } else {
                                    this.obj.fileType = 'other';
                                }
                                this.fileSrc = '/files/download/' + obj.fileId;

                                let img = new Image();
                                img.src = this.fileSrc;
                                let _this = this;
                                img.onload = function(){
                                    if (img.width > 590) {
                                        _this.imgWidth = "100%";
                                    } else {
                                        _this.imgWidth = img.width + "px";
                                    }
                                };
                            }
                            /*richText
                            $('#view-notice-body').html(obj.content);
                            richText*/
                        } else {
                            alert(res.data.msg)
                        }
                    });
                }
            },
            add() {
                if (this.comment.content === '') {
                    return;
                }
                axios.post('/foodsMenuInfoComment', this.comment).then(res => {
                    if (res.data.code === '0') {
                        this.comment.content = '';
                        this.loadComments(this.comment.foreignId);
                    } else {
                        alert(res.data.msg);
                    }
                })
            },
            loadComments(id) {
                axios.get('/foodsMenuInfoComment/findByForeignId/' + id).then(res => {
                    if (res.data.code === '0') {
                        this.comments = res.data.data;
                    } else {
                        alert(res.data.msg)
                    }
                })
            },
            getQueryVariable(key) {
                let query = window.location.search.substring(1);
                let vars = query.split("&");
                for (let i = 0; i < vars.length; i++) {
                    let pair = vars[i].split("=");
                    if (pair[0] === key) {
                        return pair[1];
                    }
                }
                return '';
            },
            logout() {
                axios.get("/logout").then(res => {
                    if(res.data.code === '0') {
                        location.href = '/front/index.html';
                    } else {
                        msg('error', res.data.msg);
                    }
                })
            },
            praise() {
                if(!this.user || !this.user.name) {
                    location.href = '/end/page/login.html';
                    return;
                }
                const data = {foodsId: this.obj.id, name: this.obj.name};
                axios.post("/praiseInfo", data).then(res => {
                    if(res.data.code === '0') {
                        if(res.data.data.id) {
                            msg("success", '点赞成功');
                            this.load();
                        }
                    } else {
                        msg('error', res.data.msg);
                    }
                })
            },
            collect() {
                if(!this.user || !this.user.name) {
                    location.href = '/end/page/login.html';
                    return;
                }
                const data = {foodsId: this.obj.id, name: this.obj.name};
                axios.post("/collectInfo", data).then(res => {
                    if(res.data.code === '0') {
                        if(res.data.data.id) {
                            msg("success", '收藏成功');
                            this.load();
                        }
                    } else {
                        msg('error', res.data.msg);
                    }
                })
            }
        }
    })
</script>
</body>
</html>
